package cn.com.zaprk.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @PackageName: cn.com.zaprk.jdbc
 * @ClassName: JDBC_06_Update
 * @Description:
 * @author: RZS
 * @date: 2021/10/20  18:02
 */
public class JDBC_06_Update {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stat = null;
        try {
            // 1、注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2、获取连接
            conn = DriverManager.getConnection("jdbc:mysql:///mydb1", "root", "root");
            // 3、创建Statement对象，用于执行sql
            stat = conn.createStatement();
            // 4、编写sql
            String sql = "update user set money=9999 where id=5";
            // 5、执行sql
            int row = stat.executeUpdate(sql);
            System.out.println(row);
            // 6、获取sql执行结果
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 7、关闭连接
            if (stat != null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
